package com.kch.web.controller.system;

import com.kch.common.annotation.Log;
import com.kch.common.core.controller.BaseController;
import com.kch.common.core.domain.AjaxResult;
import com.kch.common.core.page.TableDataInfo;
import com.kch.common.enums.BusinessType;
import com.kch.framework.util.ShiroUtils;
import com.kch.system.domain.SysUser;
import com.kch.system.domain.Train;
import com.kch.system.domain.TrainApplicants;
import com.kch.system.req.CommonReq;
import com.kch.system.service.ISysTagService;
import com.kch.system.service.ITrainApplicantsService;
import com.kch.system.service.ITrainService;
import com.kch.system.vo.TrainMemberVO;
import com.kch.system.vo.TrainVO;
import com.kch.web.config.CloumnProperties;
import io.swagger.annotations.Api;
import io.swagger.annotations.ApiOperation;
import org.apache.shiro.authz.annotation.RequiresPermissions;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Controller;
import org.springframework.ui.ModelMap;
import org.springframework.util.ObjectUtils;
import org.springframework.validation.annotation.Validated;
import org.springframework.web.bind.annotation.GetMapping;
import org.springframework.web.bind.annotation.PathVariable;
import org.springframework.web.bind.annotation.PostMapping;
import org.springframework.web.bind.annotation.RequestBody;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestParam;
import org.springframework.web.bind.annotation.ResponseBody;

import javax.validation.Valid;
import java.util.List;

@Controller
@RequestMapping("/system/train")
@Api(tags = "培训管理")
public class TrainController extends BaseController {

    private String prefix = "system/train";

    @Autowired
    private ITrainService trainService;
    @Autowired
    private ISysTagService sysTagService;
    @Autowired
    private CloumnProperties cloumnProperties;
    @Autowired
    private ITrainApplicantsService trainApplicantsService;

    @RequiresPermissions("system:train:view")
    @GetMapping()
    public String index(ModelMap mmap) {
        return prefix + "/train";
    }

    @PostMapping("/list")
    @RequiresPermissions("system:train:list")
    @ResponseBody
    public TableDataInfo list(Train train) {
        startPage();
        List<TrainVO> list = trainService.selectMeetingPage(train);
        return getDataTable(list);
    }

    @GetMapping("/status")
    @RequiresPermissions("system:train:list")
    @ResponseBody
    public AjaxResult status(Integer id) {
        try {
            trainService.updateStatus(id);
            return toAjax(true);
        } catch (Exception e) {
            return error(e.getMessage());
        }
    }

    @GetMapping("/add")
    public String add(ModelMap mmap) {
        return prefix + "/add";
    }

    @Log(title = "培训", businessType = BusinessType.INSERT)
    @PostMapping("/add")
    @ResponseBody
    public AjaxResult add(@Validated Train train) {
        train.setCreateBy(ShiroUtils.getLoginName());
        train.setCloumnCode(cloumnProperties.getTrain());
        trainService.insertSysTrain(train);
        return toAjax(true);
    }

    @Log(title = "活动类型", businessType = BusinessType.DELETE)
    @PostMapping("/remove")
    @ResponseBody
    public AjaxResult remove(String ids) {
        try {
            return toAjax(trainService.deleteTrain(ids));
        } catch (Exception e) {
            return error(e.getMessage());
        }
    }

    @GetMapping("/edit/{id}")
    public String edit(@PathVariable("id") Integer id, ModelMap mmap) {
        Train train = trainService.selectById(id);
        mmap.put("train", train);
        return prefix + "/edit";
    }

    @Log(title = "培训", businessType = BusinessType.UPDATE)
    @PostMapping("/edit")
    @ResponseBody
    public AjaxResult editSave(Train train) {
        try {
            train.setUpdateBy(ShiroUtils.getLoginName());
            train.setCloumnCode(cloumnProperties.getTrain());
            return toAjax(trainService.updateById(train));
        } catch (Exception e) {
            return toAjax(false);
        }
    }

    @GetMapping("/detail/{id}")
    public String detail(@PathVariable("id") Integer id, ModelMap mmap) {
        Train train = trainService.selectById(id);
        mmap.put("train", train);
        return prefix + "/detail";
    }

    @GetMapping("/member/{id}/{type}")
    public String meetingMember(@PathVariable("id") Integer id, @PathVariable("type") String type,
                                ModelMap mmap) {
        mmap.put("id", id);
        mmap.put("type", type);
        return prefix + "/member";
    }

    @PostMapping("/member/list")
    @ResponseBody
    public TableDataInfo memberList(@RequestParam("id") Integer id, @RequestParam("type") String type,
                                    TrainApplicants applicant) {
        TrainApplicants applicants = new TrainApplicants();
        if("1".equals(type)){
            applicants.setReviewStatus(type);
        }
        applicants.setTrainId(id);
        if (!ObjectUtils.isEmpty(applicant.getReviewStatus())) {
            applicants.setReviewStatus(applicant.getReviewStatus());
        }
        startPage();
        List<TrainMemberVO> list = trainApplicantsService.selectByTrainId(applicants);
        return getDataTable(list);
    }

    @GetMapping("/member/audit/{id}")
    public String auditMember(@PathVariable("id") Integer id, ModelMap mmap) {
        mmap.put("id", id);
        return prefix + "/audit";
    }

    @PostMapping("/member/audit")
    @ResponseBody
    public AjaxResult auditMember(TrainApplicants applicants) {
        applicants.setCreateBy(ShiroUtils.getLoginName());
        trainApplicantsService.updateById(applicants);
        return AjaxResult.success();
    }

    @PostMapping("/application")
    @ResponseBody
    @ApiOperation("培训报名,参数为培训记录id")
    public AjaxResult applicationMeeting(@RequestBody @Valid CommonReq req) {
        SysUser sysUser = ShiroUtils.getSysUser();
        trainApplicantsService.application(req, sysUser);
        return AjaxResult.success();
    }
}
